<?php

/**
 * This is the model class for table "user_table".
 *
 * The followings are the available columns in table 'user_table':
 * @property integer $id
 * @property string $user_name
 * @property string $nick_name
 * @property string $password
 */
class UserTable extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
        public $password2;
        public function tableName()
	{
		return 'user_table';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
            //如果没有设置save（)方法不会执行
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
//			array('user_name, nick_name, password', 'required'),
//			array('user_name, nick_name', 'length', 'max'=>40),
//			array('password', 'length', 'max'=>50),
//			// The following rule is used by search().
//			// @todo Please remove those attributes that should not be searched.
			array('id, user_name, nick_name, password', 'safe', 'on'=>'search'),
                        array('user_name','required','message'=>'用户名必填'),
                        array('user_name','unique','message'=>'用户名已存在'),
                        array('password','required','message'=>'密码必填'),
                         array('password2','compare','compareAttribute'=>'password','message'=>'两次密码不一致'),
                        array('user_email','email','allowEmpty'=>false,'message'=>'邮箱格式不匹配'),
                        array('user_phone_num','match','pattern'=>'/^[1-9]\d{2,12}$/','message'=>'格式不正确'),
                        array('edubg','in','range'=>array(2,3,4,5),'message'=>'请选择学历'),
                        array('hobby','checkHobby'),
                        array('id,nick_name,sex','safe'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
                        'password2'=>'确认密码',
			'user_name' => '用户名',
			'nick_name' => '昵称',
			'password' => '密码',
                       'user_email'=>'邮箱',
                    'user_phone_num'=>'手机号码',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('user_name',$this->user_name,true);
		$criteria->compare('nick_name',$this->nick_name,true);
		$criteria->compare('password',$this->password,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
                
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return UserTable the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
        //自定义验证方法
        public function checkHobby() {
            echo $this->hobby;
            $len=  strlen($this->hobby);
            if($len<3)
            $this->addError ('hobby','Please select more than two hobbies');
        }
}?>

